/*
 * StateGame.cpp
 *
 *  Created on: Jun 18, 2013
 *      Author: AT WORK
 */

#include "StateGame.h"

StateGame::StateGame() {
	_background = NULL;
	_playerAnimation = NULL;
	quitGame = false;
	_fire = NULL;
}

StateGame::~StateGame() {
	unload();
}
void StateGame::load(StateArgs arguments) {
	quitGame = false;
	nextState = SDLBase::STATENOCHANGE;
	GameArea::inicialize();
	int vet[4] { 4, 4, 4, 4 };
	_playerAnimation = new Animation("../gfx/DemonFighter.png", 100, 4, vet,
			111, true);
	_player = new Player(
			new Rect(400, 300, _playerAnimation->getWidth(),
					_playerAnimation->getHeight(), 0), _playerAnimation);
	int vet2[3] { 6, 4, 2 };
	_fire = new Animation("../gfx/fire.png", 100, 3, vet2, 100, true);
	_fire->setCurrentRow(1);
	_background = new Sprite("../gfx/background.png");
	Camera::_camera.setX(_background->getWidth() / 2 - 400);
	Camera::_camera.setY(_background->getHeight() / 2 - 300);
}

StateArgs StateGame::unload() {
	std::vector<std::string> args;
	delete (_background);
	delete (_playerAnimation);
	delete (_player);
	delete (_fire);
	return StateArgs(args);
}

void StateGame::input() {
	if (InputManager::getInstance()->QuitGame())
		quitGame = true;

}

int StateGame::update(double dt) {
	if (quitGame)
		nextState = SDLBase::STATEQUIT;
	GameArea::update(dt);
	_player->update(dt);
	_fire->update(dt);

	Camera::_camera.setX(_player->getCenterRef()->getX());
	Camera::_camera.setY(_player->getCenterRef()->getY());

	if (Camera::_camera.getX() < 0) {
		Camera::_camera.setX(0);
	}
	if (_background->getWidth() - Camera::_camera.getX()
			< GameArea::getSurface()->w) {
		Camera::_camera.setX(
				_background->getWidth() - GameArea::getSurface()->w);
	}

	if (Camera::_camera.getY() < 0) {
		Camera::_camera.setY(0);
	}
	if (_background->getHeight() - Camera::_camera.getY()
			< GameArea::getSurface()->h) {
		Camera::_camera.setY(
				_background->getHeight() - GameArea::getSurface()->h);
	}
	if (_player->getCenterRef()->getX() < 0) {
		_player->getCenterRef()->setX(0);
	}
	if (_player->getCenterRef()->getX() > _background->getWidth()) {
		_player->getCenterRef()->setX(_background->getWidth());
	}

	if (_player->getCenterRef()->getY() < 0) {
		_player->getCenterRef()->setY(0);
	}
	if (_player->getCenterRef()->getY() > _background->getHeight()) {
		_player->getCenterRef()->setY(_background->getHeight());
	}
	return nextState;
}

void StateGame::render() {
	GameArea::render();
	_background->clip(Camera::_camera.getX(), Camera::_camera.getY(),
			GameArea::getSurface()->w, GameArea::getSurface()->h);
	_background->render(0, 0, false, GameArea::getSurface());
	_player->render(Camera::_camera.getX(), Camera::_camera.getY());
	_fire->render(600 - Camera::_camera.getX(), 400 - Camera::_camera.getY(),
			true);
}

